package com.example.shushiyunsst.fragment.main.transaction.child

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import androidx.viewpager.widget.ViewPager
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.base.LazyFragment
import com.example.library_common.utils.even.MessageWrap
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.transaction.bean.TransactionUIInvokeListener
import com.example.shushiyunsst.fragment.main.transaction.child.content.AllDelegateFragment
import com.example.shushiyunsst.fragment.main.transaction.child.content.TransactionContentHeaderFragment
import com.example.shushiyunsst.fragment.main.transaction.child.content.TransactionContentListFragment
import com.google.android.material.appbar.AppBarLayout


class TransactionContentFragment : LazyFragment(), AppBarLayout.OnOffsetChangedListener,
    SwipeRefreshLayout.OnRefreshListener {

    companion object {
        fun newInstance(): TransactionContentFragment {
            val args = Bundle()
            val fragment = TransactionContentFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var transactionUIInvokeListener: TransactionUIInvokeListener? = null

    fun setTransactionUIInvokeListener(listener: TransactionUIInvokeListener?): BaseFragmentEx {
        this.transactionUIInvokeListener = listener
        return this
    }

    private lateinit var mViewPager: ViewPager

    private lateinit var mRefreshLayout: SwipeRefreshLayout

    private lateinit var mAppBarLayout: AppBarLayout

    private lateinit var mCoordinatorLayout: CoordinatorLayout

    private lateinit var labeled:View

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
    }

    override fun getLayoutId() = R.layout.transaction_content_fragment_layout


    override fun lazyInit() {
        if (!mRefreshLayout.isRefreshing) {
            mRefreshLayout.isRefreshing = true
        }
        onRefresh()
    }

    fun refresh(){
        scrollToTop()
        lazyInit()
    }

    private fun scrollToTop() {
//        mAppBarLayout.offsetTopAndBottom(0)
        //拿到 appbar 的 behavior,让 appbar 滚动
        val layoutParams: ViewGroup.LayoutParams = mAppBarLayout.layoutParams
        val behavior = (layoutParams as CoordinatorLayout.LayoutParams).behavior
        if (behavior is AppBarLayout.Behavior) {
//            拿到下方tabs的y坐标，即为我要的偏移量
//            val y: Float = labeled.y
            //注意传递负值
            behavior.topAndBottomOffset = 0
        }
    }

    override fun initView(view: View) {
        labeled= view.findViewById(R.id.labeled)
        mCoordinatorLayout = view.findViewById(R.id.mCoordinatorLayout)

        mRefreshLayout = view.findViewById(R.id.mRefreshLayout)

        mAppBarLayout = view.findViewById(R.id.mAppBarLayout)

        mAppBarLayout.addOnOffsetChangedListener(this)

        mRefreshLayout.setOnRefreshListener(this)

        mRefreshLayout.setColorScheme(R.color.nav_bar_bg_color)

        mRefreshLayout.setProgressBackgroundColor(R.color.colorAccent)

        val transactionHeader = TransactionContentHeaderFragment.newInstance()
            .setTransactionUIInvokeListener(transactionUIInvokeListener)
        loadRootFragment(R.id.transactionHeader, transactionHeader)
        mViewPager = view.findViewById(R.id.mViewPager)
        mViewPager.adapter = object : FragmentStatePagerAdapter(
            childFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        ) {
            override fun getCount() = 1

            override fun getItem(position: Int): Fragment {
                val mListFragment = TransactionContentListFragment.newInstance()
                mListFragment.setTransactionUIInvokeListener(transactionUIInvokeListener)
                return mListFragment
            }

        }

        view.findViewById<View>(R.id.allDelegate).setOnClickListener {
            start(AllDelegateFragment.newInstance())
        }

        if (!mRefreshLayout.isRefreshing) {
            mRefreshLayout.isRefreshing = true
        }

    }

    override fun handMessage(msg: MessageWrap?) {
        when (msg?.message) {
            TRANSFER_INDEX_DATA_REFRESH -> {
                if (!mRefreshLayout.isRefreshing) {
                    mRefreshLayout.isRefreshing = true
                }
            }
            transfer_index_data, transfer_index_data_failed -> {

                if (mRefreshLayout.isRefreshing) {
                    mRefreshLayout.postDelayed({
                        mRefreshLayout.isRefreshing = false
                    }, 1000L)

                }
            }
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        transactionUIInvokeListener = null
    }

    override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {

        mRefreshLayout.isEnabled = verticalOffset >=0
    }

    override fun onRefresh() {
        transactionUIInvokeListener?.requestIndex()
    }

}